<template>
  <div class="complaint">
    <div class="taber">
      <span>故障报修列表</span>
    </div>
    <div class="navquire">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
        ref="formInline"
      >
        <el-form-item label="姓名" prop="key">
          <el-input v-model="formInline.key" placeholder="职员名"></el-input>
        </el-form-item>
        <el-form-item label="职员类型" prop="type">
          <el-select v-model="formInline.type" placeholder="请选择">
            <el-option
              v-for="item in lists"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onlists">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div classs="del">
      <el-button type="primary" @click="centerDialogVisible = true"
        >添加职员</el-button
      >
      <el-button type="danger" @click="onRear">批量删除</el-button>
    </div>
    <el-table
      class="list"
      @selection-change="handleSelectionChange"
      :data="tableData"
      v-loading="loading"
      border
    >
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column fixed prop="id" label="id" width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="typename" label="职员类型" width="237">
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="180"> </el-table-column>
      <el-table-column prop="cardid" label="身份证号" width="180">
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="180">
      </el-table-column>
      <el-table-column prop="building[0].building" label="管理小区" width="180">
      </el-table-column>
      <el-table-column label="操作 " width="180" prop="id">
        <template slot-scope="scope">
          <el-row>
            <el-button type="success" size="mini">修改</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="deletelist_id(scope.row.id)"
              >删除
            </el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formInline.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="formInline.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      title="添加职员"
      :visible.sync="centerDialogVisible"
      width="35%"
      center
    >
      <el-form ref="form" :model="formquery" label-width="80px">
        <div class="images">
          <p class="image-hie"><span class="xing">*</span> 照片</p>
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="姓名">
          <el-input class="inp" v-model="formquery.name"></el-input>
        </el-form-item>
        <div class="ivs">
          <el-form-item label="身份证号">
            <el-input class="inp" v-model="formquery.cardid"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input class="inp" v-model="formquery.mobile"></el-input>
          </el-form-item>
        </div>
        <el-form-item style="height: 40px; width: 300px" label="直辖范围">
          <el-select v-model="formquery.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="height: 40px" label="职员类型">
          <el-select
            v-model="addListQuery.building"
            placeholder="请选择活动区域"
          >
            <el-option
              :label="item.name"
              v-for="(item, index) in lists"
              :key="index"
              value="shanghai"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="formquery.sex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addStaffList">确 定</el-button>
        <el-button @click="centerDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import http from "./../api/staff";
import {
  staff,
  staffdel,
  staffdelAll,
  stafftype,
  staffAdd,
} from "../api/goods";
export default {
  data() {
    return {
      radio: 0,
      value2: "",
      formInline: {
        key: "",
        type: "",
        page: 1,
        psize: 10,
      },
      tableData: [],
      multipleSelection: [],
      lists: [],
      loading: true,
      currentPage4: 4,
      total: 0,
      addListQuery: {},
      formquery: {
        id: 0,
        name: "",
        photo: null,
        type: 2,
        sex: 1,
        mobile: "",
        cardid: "",
        pass: null,
        state: 0,
        building: [
          {
            buildingid: 7,
          },
          {
            buildingid: 17,
          },
        ],
      },
      imageUrl: "",
      centerDialogVisible: false,
    };
  },
  mounted() {
    this.onlists();
    this.onstafftype();
  },
  methods: {
    async addStaffList() {
      console.log(this.formquery);
      const { errCode, data } = await http.addStaff(this.formquery);
      if (errCode == 10000) {
        this.$message.success("添加成功");
        this.loading = false;
        this.onlists();
        this.centerDialogVisible = false;
      }
    },
    async onstafftype() {
      const { data, errCode } = await http.stafftype({
        page: this.page,
        psize: this.psize,
      });
      if (errCode == "10000") {
        this.lists = data.list;
      }
    },
    //列表渲染
    async onlists() {
      const { errCode, data } = await http.getStaffList(this.formInline);
      console.log(errCode);
      if (errCode == 10000) {
        this.tableData = data.list;
        this.loading = false;
        this.total = data.counts;
      }

      // console.log(this.formInline)
      // co nst formInline = this.formInline/*  */

      /*  const { data: res, errCode } = await questions({formInline}) */ // console.log(formInline)
      /*  console.log(res, errCode, res.pageCounts, res.pageSize);
      if (errCode == "10000") {
        this.loading = false;
        this.tableData = res.list;
        this.page = res.pageSize;
        this.psize = res.pageCounts;
      } */
      //   console.log(this.formInline.key,this.formInline.type)
      /*       const { data, errCode } = await staff({
        params:this.formInline
      });
      console.log(data);
      if (errCode == 10000) {
        this.tableData = data.list;
        this.loading = false;
        this.total = data.counts
      } */

      /*  this.tableData = res.data.data.list;
          this.loading = false;
          this.total = res.data.data.counts; */
      // this.formInline.page = res.data.data.pageCounts;
      // this.formInline.psize = res.data.data.pageSize;
      // console.log(res.data.data.counts)

      // console.log(this.total, this.formInline.page, this.formInline.psize);
      /*  });*/
      // console.log(this.formInline.page,this.formInline.psize,this.total,)
    },
    //单条删除
    async deletelist_id(ids) {
      this.loading = true;
      let id = ids;
      console.log(id);
      const { errCode } = await http.staffdel({ id: id });
      if (errCode == 10000) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.loading = false;
            this.onlists();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //批量删除
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(val);
    },
    onRear() {
      if (this.multipleSelection == 0) {
        this.$message({
          showClose: true,
          message: "请勾选需要删除的数据",
          type: "error",
        });
      }
      let arr = [];
      this.multipleSelection.forEach((item) => {
        arr.push(item.id);
      });
      // console.log(arr)
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          http.staffdelAll({ ids: arr }).then((res) => {
            if (res.errCode == 10000) {
              this.onlists();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    idqwe(id) {
      console.log(id);
    },
    handleSizeChange(val) {
      this.formInline.psize = val;
      this.onlists();
    },
    handleCurrentChange(val) {
      this.formInline.page = val;
      this.onlists();
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.inp {
  width: 215px;
}
.complaint {
  padding: 20px;
}

.taber {
  color: rgb(66, 127, 244);
  height: 50px;
  border-bottom: 1px solid #cccc;
}
.navquire {
  margin-top: 30px;
}
.list {
  width: 100%;
  margin-top: 20px;
}
.el-table {
  width: 100% !important;
}
.el-table th.el-table__cell > .cell {
  text-align: center !important;
}
.el-table td.el-table__cell div {
  text-align: center !important;
}

.menu {
  height: 100vh;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 78px;
  height: 78px;
  line-height: 78px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.images {
  display: flex;
  line-height: 80px;
}
.image-hie {
  margin-right: 40px;
}
.xing {
  color: #ff0000;
}
.ivs {
  width: 10;
  float: right;
  margin-top: -60px;
}
</style>          